package com.egao.common.module.cms.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.cms.entity.Category;
import com.egao.common.module.cms.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 栏目管理
 * @author pyx
 */
@Api(tags = "栏目管理")
@RestController
@RequestMapping("${api}")
public class CategoryController extends BaseController {
    @Autowired
    private CategoryService categoryService;

    @PreAuthorize("hasAuthority('cms:category:list')")
    @ApiOperation("查询树形栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "栏目类型", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "homeFlag", value = "首页显示", required = false, dataType = "string", paramType = "query"),
    })
    @OperLog(value = "栏目管理", desc = "查询树形栏目")
    @GetMapping("/cms/category/tree")
    public JsonResult tree(HttpServletRequest request) {
        PageParam<Category> pageParam = new PageParam<>(request);
        pageParam.setSize(1000);
        pageParam.setDefaultOrder(new String[]{"sort"}, new String[]{"create_time"});
        return categoryService.treeAllList(pageParam);
    }

    @PreAuthorize("hasAuthority('cms:category:list')")
    @ApiOperation("查询单个栏目")
    @OperLog(value = "栏目管理", desc = "查询单个栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "栏目id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/cms/category/get")
    public JsonResult listGetId(Integer id) {
        return categoryService.getId(id);
    }

    @PreAuthorize("hasAuthority('cms:category:insert')")
    @ApiOperation("添加栏目")
    @OperLog(value = "栏目管理", desc = "添加栏目", result = true)
    @PostMapping("/cms/category/insert")
    public JsonResult insert(@RequestBody Category category) {
        return categoryService.insertCategory(category);
    }

    @PreAuthorize("hasAuthority('cms:category:update')")
    @ApiOperation("修改栏目")
    @OperLog(value = "栏目管理", desc = "修改栏目", result = true)
    @PutMapping("/cms/category/update")
    public JsonResult update(@RequestBody Category category) {
        return categoryService.updateCategory(category);
    }

    @PreAuthorize("hasAuthority('cms:category:delete')")
    @ApiOperation("删除栏目")
    @OperLog(value = "栏目管理", desc = "删除栏目", result = true)
    @DeleteMapping("/cms/category/delete")
    public JsonResult delete(Integer id) {
        return categoryService.deleteById(id);
    }

    @PreAuthorize("hasAuthority('cms:category:list')")
    @ApiOperation("文章搜索所属栏目")
    @OperLog(value = "栏目管理", desc = "查询所有二级栏目")
    @GetMapping("/cms/category/list/children")
    public JsonResult childrenList() {
        return JsonResult.ok().setData(categoryService.childrenList());
    }

}
